package com.newsmanagement.mapper;

import com.newsmanagement.domain.pojo.SlauBatch;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface SlauSlauBatchMapper {

    @Select("SELECT COUNT(*) FROM slau_batch WHERE batch_id = #{batchId}")
    int checkBatchIdExists(@Param("batchId") String batchId);

    // 检查屠宰企业编号是否存在（type=2）
    @Select("SELECT COUNT(*) FROM node_info WHERE code = #{slauId} AND type = 2")
    int checkSlauIdExists(@Param("slauId") String slauId);

    @Insert("INSERT INTO slau_batch (batch_id, slau_id, fb_id, type, qua_id, test_name, batch_date, state, remarks) " +
            "VALUES (#{batchId}, #{slauId}, #{fbId}, #{type}, #{quaId}, #{testName}, #{batchDate}, #{state}, #{remarks})")
    int insertSlauBatch(SlauBatch slauBatch);

    // 根据批号查询屠宰批号
    @Select("SELECT " +
            "sb_id as sbId, " +
            "batch_id as batchId, " +
            "slau_id as slauId, " +
            "fb_id as fbId, " +
            "type, " +
            "qua_id as quaId, " +
            "test_name as testName, " +
            "batch_date as batchDate, " +
            "state, " +
            "remarks " +
            "FROM slau_batch WHERE batch_id = #{batchId}")
    SlauBatch selectSlauBatchById(@Param("batchId") String batchId);

    // 查询所有屠宰批号 - 修改：使用别名匹配实体类属性名
    @Select("SELECT " +
            "sb_id as sbId, " +
            "batch_id as batchId, " +
            "slau_id as slauId, " +
            "fb_id as fbId, " +
            "type, " +
            "qua_id as quaId, " +
            "test_name as testName, " +
            "batch_date as batchDate, " +
            "state, " +
            "remarks " +
            "FROM slau_batch WHERE state != 4 ORDER BY batch_date DESC")
    List<SlauBatch> selectAllSlauBatches();

    // 根据状态查询屠宰批号 - 修改：使用别名匹配实体类属性名
    @Select("SELECT " +
            "sb_id as sbId, " +
            "batch_id as batchId, " +
            "slau_id as slauId, " +
            "fb_id as fbId, " +
            "type, " +
            "qua_id as quaId, " +
            "test_name as testName, " +
            "batch_date as batchDate, " +
            "state, " +
            "remarks " +
            "FROM slau_batch WHERE state = #{state} ORDER BY batch_date DESC")
    List<SlauBatch> selectSlauBatchesByState(@Param("state") Integer state);

    // 获取状态统计
    @Select("SELECT state, COUNT(*) as count FROM slau_batch GROUP BY state")
    List<Map<String, Object>> selectStatusCounts();

    // 删除屠宰批号
    @Delete("DELETE FROM slau_batch WHERE batch_id = #{batchId}")
    int deleteSlauBatch(@Param("batchId") String batchId);

    @Update("<script>" +
            "UPDATE slau_batch " +
            "<set>" +
            "  <if test='slauId != null'>slau_id = #{slauId},</if>" +
            "  <if test='fbId != null'>fb_id = #{fbId},</if>" +
            "  <if test='type != null'>type = #{type},</if>" +
            "  <if test='quaId != null'>qua_id = #{quaId},</if>" +
            "  <if test='testName != null'>test_name = #{testName},</if>" +
            "  <if test='batchDate != null'>batch_date = #{batchDate},</if>" +
            "  <if test='state != null'>state = #{state},</if>" +  // 添加 state 字段
            "</set>" +
            "WHERE batch_id = #{batchId}" +
            "</script>")
    int updateSlauBatchSelective(SlauBatch slauBatch);
}